package com.azkj.album.utils;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import com.azkj.album.MyApplication;
import com.azkj.album.R;

import java.util.HashMap;

public class SoundUtils {
    private static SoundUtils instance;
    private Context mContext;

    private static AudioAttributes attr;
    public static SoundPool soundpool;
    public static HashMap<Integer, Integer> soundmap;

    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            attr = new AudioAttributes.Builder()           //设置音效相关属性
                    .setUsage(AudioAttributes.USAGE_GAME)                 // 设置音效使用场景
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)  // 设置音效的类型
                    .build();
            soundpool = new SoundPool.Builder()           // 创建SoundPool对象
                    .setAudioAttributes(attr) // 设置音效池的属性
                    .setMaxStreams(10) // 设置最多可容纳10个音频流，
                    .build();
        } else {
            soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 8);
        }

        //创建一个HashMap对象,将要播放的音频流保存到HashMap对象中
        soundmap = new HashMap<Integer, Integer>();
        soundmap.put(0, soundpool.load(MyApplication.getApplication(), R.raw.sound_click, 1));
        soundmap.put(1, soundpool.load(MyApplication.getApplication(), R.raw.sound_back, 1));
        soundmap.put(2, soundpool.load(MyApplication.getApplication(), R.raw.botton_click, 1));


    }


    public static SoundUtils getInstance() {
        if (instance == null) {
            instance = new SoundUtils();
        }
        return instance;
    }

    private SoundUtils() {
        mContext = MyApplication.getApplication();
    }

    public void play() {
        soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);
    }


}
